package com.gem.netty.message;

import com.gem.entity.*;
import com.gem.enums.MessageColorType;
import com.gem.factory.game.GameFactory;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.CurrentUser;
import com.gem.netty.message.swap.AbstractSwapBase;
import com.gem.netty.message.swap.GetNobility;
import com.gem.netty.service.common.IsCurrentUserRoundService;
import com.gem.netty.service.nobility.GetNobilityVerifyService;
import com.gem.netty.service.nobility.IsExistNobilityService;
import com.gem.netty.service.nobility.IsSwapNobilityService;
import com.gem.netty.service.nobility.IsUserOwnNobilityService;
import com.gem.netty.session.GroupSessionFactory;
import com.gem.pipeline.BusinessProcess;
import com.gem.pipeline.ProcessContext;
import com.gem.pipeline.ProcessController;
import com.gem.utils.R;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Xhy
 * CreateTime: 2022-09-24 17:27
 * 获取贵族
 */
public class GetNobilityMessage implements Message {

    @Override
    public void handler(ClientContent content) {

        ChannelHandlerContext ctx = content.getCtx();
        Game game = GameFactory.get(content.getRoomId());
        String roomId = game.getRoomId();
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        // 将请求中的贵族信息存入当前用户回合信息
        user.setCurrentGetNobilityInfo(content.getContent().getNobility());
        // 责任链模式
        AbstractSwapBase getNobility = new GetNobility();
        R r = getNobility.doSwap(content);
        if (!r.getState()){
            ctx.writeAndFlush(new TextWebSocketFrame(r.toString()));
            return;
        }

        // 贵族库存减少
        reduceGameNobility(content);
        // 加分数
        addScore(user);
        // 用户添加贵族
        addNobility(content);
        List<Channel> channels = GroupSessionFactory.getGroupSession().getMembersChannel(content.getRoomId());
        // 兑换后是否达到15分
        if (is15score(user)){
            GameFactory.notifyUsers(channels,R.ok().data("color", MessageColorType.SYSTEM_MESSAGE.type).message(user.getName() +"这哥们有15分了!").type(content.getType()),roomId);
        }
        GameFactory.notifyUsers(channels,R.ok().data("color",MessageColorType.GET_NOBILITY.type).message(user.getName() +"拿取了贵族").type(content.getType()),roomId);
    }

    private void addNobility(ClientContent content) {
        Game game = GameFactory.get(content.getRoomId());
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        Nobility nobility = Nobility.get(content.getContent().getNobility().getNo());
        user.setNobility(nobility);
    }


    private boolean is15score(CurrentUser user){
        return user.getOwnScore() >=15;
    }

    /**
     * 加分数
     * @param user
     */
    private void addScore(CurrentUser user) {
        user.setOwnScore(user.getOwnScore()+3);
    }

    /**
     * 减少贵族库存
     * @param content
     */
    private void reduceGameNobility(ClientContent content){
        Game game = GameFactory.get(content.getRoomId());
        int i = 0;
        List<Nobility> nobilities = game.getNobilities();
        Nobility nobility = Nobility.get(content.getContent().getNobility().getNo());
        for (;i<nobilities.size();i++){
            if (nobilities.get(i).getNo() == nobility.getNo()){
                break;
            }
        }
        nobilities.remove(i);
    }

}
